using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.AcmsControls.ContentControls.FrontEnd
{
    /// <summary>
    /// Renders image tag surounded an anchor tag
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:AcmsImageLink runat=server></{0}:AcmsImageLink>")]
    public class AcmsImageLink : HyperLink
    {
        /// <summary>
        /// Acms File Id to render
        /// </summary>
        public int? FileId { get; set; }
        
        /// <summary>
        /// Height to render the image
        /// </summary>
        public int RenderHeight { get; set; }

        /// <summary>
        /// Width to render the image
        /// </summary>
        public int RenderWidth { get; set; }

        public override void DataBind()
        {
            base.DataBind();
            OnDataBound();
        }

        protected void OnDataBound()
        {
            if (DataBound != null)
            {
                DataBound(this, EventArgs.Empty);
            }
        }

        public event EventHandler DataBound;

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (FileId != null && FileId.Value != 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
                writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ResolveUrl(this.NavigateUrl));
                if (!string.IsNullOrEmpty(this.CssClass))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
                }
                if (!string.IsNullOrEmpty(this.Target))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Target, this.Target);
                }
                writer.AddAttribute(HtmlTextWriterAttribute.Alt, this.ToolTip);
                writer.RenderBeginTag(HtmlTextWriterTag.A);

                writer.AddAttribute(HtmlTextWriterAttribute.Src, Page.ResolveUrl(string.Format("~/File.aspx?Id={0}&width={1}&height={2}", FileId.Value, RenderWidth, RenderHeight)));
                writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "_Img"));
                writer.AddAttribute(HtmlTextWriterAttribute.Alt, this.ToolTip);
                writer.RenderBeginTag(HtmlTextWriterTag.Img);

                writer.RenderEndTag();
                writer.RenderEndTag();

            }
        }

        protected override object SaveViewState()
        {
            ViewState["FileId"] = FileId;
            ViewState["RenderWidth"] = RenderWidth;
            ViewState["RenderHeight"] = RenderHeight;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (ViewState["FileId"] is int?)
            {
                FileId = (int?)ViewState["FileId"];
            }
            if (ViewState["RenderWidth"] is int)
            {
                RenderWidth = (int)ViewState["RenderWidth"];
            }
            if (ViewState["RenderHeight"] is int)
            {
                RenderHeight = (int)ViewState["RenderHeight"];
            }
        }
    }
}
